package top.liubaiblog.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.liubaiblog.blog.mapper.ArticleCollectionMapper;
import top.liubaiblog.blog.pojo.Article;
import top.liubaiblog.blog.pojo.ArticleCollection;
import top.liubaiblog.blog.service.ArticleCollectionService;
import top.liubaiblog.blog.threadlocal.UserThreadLocal;
import top.liubaiblog.blog.util.PojoUtils;
import top.liubaiblog.blog.vo.ArticleVo;
import top.liubaiblog.blog.vo.LoginUserVo;
import top.liubaiblog.blog.vo.PageArticleVo;
import top.liubaiblog.blog.vo.param.ArticleCollectionParam;
import top.liubaiblog.blog.vo.param.PageParams;

import java.util.List;
import java.util.Objects;

/**
* @author 13326
* @description 针对表【ms_article_collection】的数据库操作Service实现
* @createDate 2022-12-10 15:15:17
*/
@Service
@Slf4j
public class ArticleCollectionServiceImpl extends ServiceImpl<ArticleCollectionMapper, ArticleCollection>
    implements ArticleCollectionService{

    @Override
    public boolean isCollected(Long articleId) {
        // 合法性判断
        if (Objects.isNull(articleId)) {
            return false;
        }
        // 获取登录用户
        LoginUserVo loginUser = UserThreadLocal.get();
        if (Objects.isNull(loginUser)) {
            return false;
        }
        // 从数据库中查找当前用户是否收藏了此文
        LambdaQueryWrapper<ArticleCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollection::getUserId, loginUser.getId());
        wrapper.eq(ArticleCollection::getArticleId, articleId);
        long count = count(wrapper);
        return count > 0;
    }

    @Override
    public boolean updateByParam(ArticleCollectionParam collectionParam) {
        // 合法性判断
        if (Objects.isNull(collectionParam) ||
                Objects.isNull(collectionParam.getArticleId()) ||
                Objects.isNull(collectionParam.getCollected())) {
            return false;
        }
        // 获取当前登录用户
        LoginUserVo loginUser = UserThreadLocal.get();
        // 判断当前文章收藏状态
        boolean collected = isCollected(collectionParam.getArticleId());
        // 如果当前收藏状态和目的收藏状态一致，则无需修改
        if (Objects.equals(collected, collectionParam.getCollected())) {
            log.warn("用户【{}】对文章【{}】的收藏状态已为【{}】，无需修改", loginUser.getId(),
                    collectionParam.getArticleId(),
                    collected);
            return true;
        }
        boolean result = false;
        // 执行修改操作
        if (collectionParam.getCollected()) {
            // 收藏文章
            ArticleCollection articleCollection = new ArticleCollection();
            articleCollection.setArticleId(collectionParam.getArticleId());
            articleCollection.setUserId(loginUser.getId());
            result = save(articleCollection);
        } else {
            // 取消文章收藏
            LambdaQueryWrapper<ArticleCollection> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ArticleCollection::getArticleId, collectionParam.getArticleId());
            wrapper.eq(ArticleCollection::getUserId, loginUser.getId());
            result = remove(wrapper);
        }
        return result;
    }

    @Override
    public PageArticleVo pageByParam(PageParams pageParams) {
        // 获取当前登录用户
        LoginUserVo loginUserVo = UserThreadLocal.get();
        // 分页数据
        Page<Article> articlePage = new Page<>(pageParams.getPage(), pageParams.getPageSize());
        // 获取用户收藏的文章列表
        getBaseMapper().selectByParam(articlePage, loginUserVo.getId(), pageParams.getSortName());
        // 封装数据
        PageArticleVo pageArticleVo = new PageArticleVo();
        List<ArticleVo> articles = PojoUtils.article.copyList(articlePage.getRecords(), true, true);
        pageArticleVo.setArticles(articles);
        pageArticleVo.setPageSize(articlePage.getSize());
        pageArticleVo.setTotal(articlePage.getTotal());
        return pageArticleVo;
    }
}




